lapis = require "lapis"
import respond_to from require "lapis.application"
import Model from require "lapis.db.model"

class Article extends Model
    @table_name:=> "articles"

class ArticleApplication extends lapis.Application
    @enable "etlua"

    [article_new:"/article/new"]: =>
        render: true

    [article_list: "/article/list"]: =>
        @articles = Article\select!
        render: true 

    [article_delete: "/article/delete/:id"]: respond_to {
        before: =>
            @article = Article\find @params.id
            @write status: 404, "Not Found" unless @article

        POST: =>
            @article\delete!
    }

    [article_item: "/article/edit/:id"]: respond_to {
        before: =>
            @article = Article\find @params.id
            @write status: 404, "Not Found" unless @article

        GET: =>
            render: true

        POST: =>
            @article\update{
                title: @params.title
                content: @params.content
            }
            render: "item"
    }
    
    [article_create: "/article/create"]: respond_to {
        POST: =>
            article = Article\create{
                title: @params.title
                content: @params.content
            }
            @articles = Article\select!
            render: "list"
    }
